package com.loma.common.model.http

import rxhttp.wrapper.annotation.Parser
import rxhttp.wrapper.entity.ParameterizedTypeImpl
import rxhttp.wrapper.parse.AbstractParser
import rxhttp.wrapper.utils.convert
import java.io.IOException
import java.lang.reflect.Type
import kotlin.jvm.Throws

@Parser(name = "Response")
open class ResponseParser<T> : AbstractParser<T> {

    //以下两个构造方法是必须的
    protected constructor() : super()
    constructor(type: Type) : super(type)

    @Throws(IOException::class)
    override fun onParse(response: okhttp3.Response): T {
        val type: Type = ParameterizedTypeImpl[Response::class.java, mType] //获取泛型类型
        val res: Response<T> = response.convert(type)

        if (res.code != 200) { //code不等于200，说明数据不正确，抛出异常
            throw ApiException(res.code, res.msg!!)
        }else{
            return res.data!! //获取data字段

        }




    }
}
